#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Premiere.DTO;
using Premiere.Window;
using System.Collections;
using Premiere.Framework;
using Gizmox.WebGUI;

#endregion

namespace PremiereMX
{
    public partial class UserControlPart : UserControl, IUserControlPart
    {
        PartLogic m_logic;
        IMainForm m_mainForm;
        Hashtable m_items = new Hashtable();
        bool m_readonly = false;
        bool b_init = false;
        public UserControlPart(string name, IMainForm mainForm)
        {
            InitializeComponent();
            m_logic = new PartLogic(mainForm, this);
            m_mainForm = mainForm;
            this.Name = name;
            this.Dock = DockStyle.Fill;
            InitButtonClick();
            textBoxFilter.Focus();

            dataGridViewUOM.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridViewUOM_CellValidating);
            dataGridViewUOM.CellValidated+=new DataGridViewCellEventHandler(dataGridViewUOM_CellValidated);
            dataGridViewVendorPartName.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridViewVendorPartName_CellValidating);
            dataGridViewVendorPartName.CellValidated += new DataGridViewCellEventHandler(dataGridViewVendorPartName_CellValidated);
            dataGridViewService.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridViewService_CellValidating);
            dataGridViewService.CellValidated += new DataGridViewCellEventHandler(dataGridViewService_CellValidated);

           // dataGridViewUOM.Rows.CollectionChanged += new CollectionChangeEventHandler(Rows_CollectionChanged);
           // dataGridViewVendorPartName.Rows.CollectionChanged += new CollectionChangeEventHandler(Rows_CollectionChanged);
           // dataGridViewService.Rows.CollectionChanged += new CollectionChangeEventHandler(Rows_CollectionChanged);

            dataGridViewUOM.CellValidated+=new DataGridViewCellEventHandler(dataGridViewUOM_CellValidated);
        }

        void Rows_CollectionChanged(object sender, CollectionChangeEventArgs e)
        {
            
        }

        void dataGridViewService_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            dataGridViewService.Rows[e.RowIndex].ErrorText = "";
            //ServicePartDTO unit = (ServicePartDTO)dataGridViewService.Rows[e.RowIndex].Tag;
            //if (unit == null) return;
            //if (e.ColumnIndex == 0)
            //{
            //    unit.PartService = (PartDTO)dataGridViewService[e.ColumnIndex, e.RowIndex].Value;
            //}
            //if (e.ColumnIndex == 1)
            //{
            //    unit.Name = dataGridViewService[e.ColumnIndex, e.RowIndex].Value.ToString();
            //}
            
        }

        void dataGridViewService_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == sdataGridViewComboBoxColumn1.Index)
            {
                foreach (DataGridViewRow v in dataGridViewService.Rows)
                {
                    if (e.RowIndex == v.Index) continue;
                    if (dataGridViewService[sdataGridViewComboBoxColumn1.Index, v.Index].Value == null) continue;
                    string vendor = dataGridViewService[sdataGridViewComboBoxColumn1.Index, v.Index].Value.ToString();
                    if (vendor == e.FormattedValue.ToString())
                    {
                        dataGridViewService.Rows[e.RowIndex].ErrorText = "Vendor has been add";
                        e.Cancel = true;
                    }
                }
            }
        }

        void dataGridViewVendorPartName_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            dataGridViewVendorPartName.Rows[e.RowIndex].ErrorText = "";
            //VendorPartNameDTO unit = (VendorPartNameDTO)dataGridViewVendorPartName.Rows[e.RowIndex].Tag;
            //if (unit == null) return;
            //if (e.ColumnIndex == 0)
            //{
            //    unit.Vendor = (VendorDTO)dataGridViewVendorPartName[e.ColumnIndex, e.RowIndex].Value;
            //}
            //if (e.ColumnIndex == 1)
            //{
            //    unit.Name = dataGridViewVendorPartName[e.ColumnIndex, e.RowIndex].Value.ToString();
            //}
        }

        void dataGridViewVendorPartName_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == vdataGridViewComboBoxColumnVendorName.Index)
            {
                foreach (DataGridViewRow v in dataGridViewVendorPartName.Rows)
                {
                    if (e.RowIndex == v.Index) continue;
                    if (dataGridViewVendorPartName[vdataGridViewComboBoxColumnVendorName.Index, v.Index].Value == null) continue;
                    string vendor = dataGridViewVendorPartName[vdataGridViewComboBoxColumnVendorName.Index, v.Index].Value.ToString();
                    if (vendor == e.FormattedValue.ToString())
                    {
                        dataGridViewVendorPartName.Rows[e.RowIndex].ErrorText = "Vendor has been add";
                        e.Cancel = true;
                    }
                }
            }
            //if (e.ColumnIndex == 0)
            //{
            //    VendorDTO selectedvendor = (VendorDTO)dataGridViewVendorPartName[0, e.RowIndex].Value;
            //    foreach (DataGridViewRow rw in dataGridViewVendorPartName.Rows)
            //    {
            //        if (rw.Index == e.RowIndex) continue;
            //        VendorDTO vendor = (VendorDTO)dataGridViewVendorPartName[0, rw.Index].Value;
            //        if (vendor == null) continue;
            //        if (selectedvendor.Id == vendor.Id)
            //        {
            //            dataGridViewVendorPartName.Rows[e.RowIndex].ErrorText = "Vendor allready add";
            //            e.Cancel = true;
            //        }
            //    }
            //}
        }

        void dataGridViewUOM_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            dataGridViewUOM.Rows[e.RowIndex].ErrorText = ""; 
            if (Unit == null) return;
            dataGridViewUOM[udataGridViewTextBoxColumnOrigUnit.Index, e.RowIndex].Value = Unit.Code;

            //UnitConversionDTO unit = (UnitConversionDTO)dataGridViewUOM.Rows[e.RowIndex].Tag;
            //if (unit == null) return;
            //if (e.ColumnIndex == 0)
            //{
            //    unit.ConversionQty = Convert.ToDecimal(dataGridViewUOM[e.ColumnIndex, e.RowIndex].Value);
            //}
            //if (e.ColumnIndex == 1)
            //{
            //    unit.ConversionUnit = (UnitDTO)dataGridViewUOM[e.ColumnIndex, e.RowIndex].Value;
            //}
            //if (e.ColumnIndex == 2)
            //{
            //    unit.OriginalQty = Convert.ToDecimal(dataGridViewUOM[e.ColumnIndex, e.RowIndex].Value);
            //}
            //if (e.ColumnIndex == 4)
            //{
            //    unit.CostPrice = Convert.ToDecimal(dataGridViewUOM[e.ColumnIndex, e.RowIndex].Value);
            //}
            //if (e.ColumnIndex == 5)
            //{
            //    unit.SellPrice = Convert.ToDecimal(dataGridViewUOM[e.ColumnIndex, e.RowIndex].Value);
            //}
        }

        void dataGridViewUOM_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if ((e.ColumnIndex == udataGridViewTextBoxColumnConvUnit.Index) ||
                (e.ColumnIndex == udataGridViewTextBoxColumnCostPrice.Index) ||
                (e.ColumnIndex == udataGridViewTextBoxColumnOrigQty.Index) ||
                (e.ColumnIndex == udataGridViewTextBoxColumnSellPrice.Index)
                )
            {
                try
                {
                    Convert.ToDecimal(e.FormattedValue.ToString());
                }
                catch (Exception x)
                {
                    dataGridViewUOM.Rows[e.RowIndex].ErrorText =
                                  x.Message;
                    e.Cancel = true;
                }
            }
            if (e.ColumnIndex == udataGridViewComboBoxColumnConvUnit.Index)
            {
                foreach (DataGridViewRow v in dataGridViewUOM.Rows)
                {
                    if (e.RowIndex == v.Index) continue;
                    if (dataGridViewUOM[udataGridViewComboBoxColumnConvUnit.Index, v.Index].Value == null) continue;
                    string unit = dataGridViewUOM[udataGridViewComboBoxColumnConvUnit.Index, v.Index].Value.ToString();
                    if (unit == e.FormattedValue.ToString())
                    {
                        dataGridViewUOM.Rows[e.RowIndex].ErrorText = "Unit has been add";                                                         
                        e.Cancel = true;
                    }
                }
            }
            //if (e.ColumnIndex == 1)
            //{
            //    if (Unit == null)
            //    {
            //        dataGridViewUOM.Rows[e.RowIndex].ErrorText = "Please select Unit Master";
            //        e.Cancel = true;
            //    }
            //    UnitDTO unit = (UnitDTO)dataGridViewUOM[1, e.RowIndex].Tag;
            //    if (unit == null)
            //    {
            //        e.Cancel = false;
            //        return;
            //    }
            //    if (unit.Id == Unit.Id)
            //    {
            //        dataGridViewUOM.Rows[e.RowIndex].ErrorText = "Unit COnversion cannot be same as Part Master Unit";
            //        e.Cancel = true;
            //    }
            //    foreach (DataGridViewRow rw in dataGridViewUOM.Rows)
            //    {
            //        if (rw.Index == e.RowIndex) continue;
            //        UnitDTO uom = (UnitDTO)dataGridViewUOM[1,rw.Index].Tag;
            //        if (uom == null) continue;
            //        if (uom.Id == unit.Id)
            //        {
            //            dataGridViewUOM.Rows[e.RowIndex].ErrorText = "Unit allready add";
            //            e.Cancel = true;
            //        }
            //    }
            //}
            //if ((e.ColumnIndex == 4) || (e.ColumnIndex == 5))
            //{
            //    if (Currency == null)
            //    {
            //        dataGridViewUOM.Rows[e.RowIndex].ErrorText = "Please setup Part Currency";
            //        e.Cancel = true;
            //    }
            //    dataGridViewUOM[e.ColumnIndex, e.RowIndex].Style.Format = "N" + Currency.DecimalPlace;
            //}
        }

        private void InitButtonClick()
        {
            toolBarButtonSave.Click += new EventHandler(toolBarButtonSave_Click);
            toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            toolBarButtonEdit.Click += new EventHandler(toolBarButtonEdit_Click);
            toolBarButtonNew.Click += new EventHandler(toolBarButtonNew_Click);
            toolBarButtonPrint.Click += new EventHandler(toolBarButtonPrint_Click);
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);

            buttonSave.Click += new EventHandler(toolBarButtonSave_Click);
            buttonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            buttonEdit.Click += new EventHandler(toolBarButtonEdit_Click);
            buttonNew.Click += new EventHandler(toolBarButtonNew_Click);
        }
        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            Refresh();
        }

        void toolBarButtonPrint_Click(object sender, EventArgs e)
        {
            m_logic.Print();
        }

        void toolBarButtonNew_Click(object sender, EventArgs e)
        {
            m_logic.New();
        }

        void toolBarButtonEdit_Click(object sender, EventArgs e)
        {
            m_logic.Edit();
        }

        void toolBarButtonDelete_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Are you sure to delete this record?", "Confirmation", MessageBoxButtons.YesNo,
                 MessageBoxIcon.Question, new EventHandler(DeleteConfirmation));
        }
        void DeleteConfirmation(object sender, EventArgs e)
        {
            MessageBoxWindow msg = (MessageBoxWindow)sender;
            if (msg.DialogResult == DialogResult.Yes)
                m_logic.Delete();
        }

        void toolBarButtonSave_Click(object sender, EventArgs e)
        {
            m_logic.Save();
        }
        private void listViewRecords_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewRecords.SelectedItem == null) return;
            PartDTO user = (PartDTO)listViewRecords.SelectedItem.Tag;
            m_logic.UpdateSubject(user);
        }


        #region IUserControlPart Members

        public string Code
        {
            get
            {
                return textBoxCode.Text;
            }
            set
            {
                textBoxCode.Text = value;
            }
        }

        public string Named
        {
            get
            {
                return textBoxName.Text;
            }
            set
            {
                textBoxName.Text = value;
                tabPage1.Text = Code + " - " + value;
            }
        }

        public DateTime LastUpdate
        {
            set { statusBar1.Text = statusBar1.Text + " Last Updated on : " + value.ToString("dd-MMM-yyyy"); }
        }

        public string ComputerName
        {
            set { statusBar1.Text = statusBar1.Text + " Computer Name : " + value; }
        }

        public string UserUpdateBy
        {
            set { statusBar1.Text = statusBar1.Text + " Updated By : " + value; }
        }

        public void ShowMessage(string mess)
        {
            MessageBox.Show(mess, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public void UpdateList(System.Collections.IList list)
        {
            foreach (PartDTO curr in list)
            {
                if (m_items.ContainsKey(curr.Id))
                {
                    ListViewItem item = (ListViewItem)m_items[curr.Id];
                    item.SubItems[0].Text = curr.Code;
                    item.SubItems[1].Text = curr.Name;
                    item.SubItems[2].Text = curr.PartGroup.Code;
                    //item.SubItems[3].Text = curr.LastUpdate.ToString("dd-MMM-yyyy hh:mm:ss");
                    //item.SubItems[4].Text = curr.UserName;
                }
                else
                {
                    ListViewItem item = listViewRecords.Items.Add(curr.Code);
                    item.SubItems.Add(curr.Name);
                    item.SubItems.Add(curr.PartGroup.Code);
                    //item.SubItems.Add(curr.LastUpdate.ToString("dd-MMM-yyyy hh:mm:ss"));
                    //item.SubItems.Add(curr.UserName);
                    item.Tag = curr;
                    m_items.Add(curr.Id, item);
                }
            }
        }

        public void SetMainForm(IMainForm mainform)
        {
            m_mainForm = mainform;
        }

        public void UpdateButton()
        {
            m_mainForm.UpdateButton(toolBarMain.Buttons);
        }

        public void Init()
        {
            if (b_init) { return; } b_init = true; 
            m_logic.Init();
            partTypecomboBox.DataSource = Enum.GetValues(typeof(PartType));
            costMethodcomboBox.DataSource = Enum.GetValues(typeof(CostMethod));
            labelCategory1.Text = m_mainForm.GetGeneralSetup().PartCategory1ColumnName;
            labelCategory2.Text = m_mainForm.GetGeneralSetup().PartCategory2ColumnName;
            labelCategory3.Text = m_mainForm.GetGeneralSetup().PartCategory3ColumnName;
            partgroupcomboBox.DataSource = m_logic.PartGroupList;
            currencycomboBox.DataSource = m_logic.CurrencyList;
            unitcomboBox.DataSource = m_logic.UnitList;

            category1comboBox.DataSource = m_logic.PartCategoryList1;
            category2comboBox.DataSource = m_logic.PartCategoryList2;
            category3comboBox.DataSource = m_logic.PartCategoryList3;

            partgroupcomboBox.Text = ServiceRegistry.GetInstance().GetDefaultDto(partgroupcomboBox.DataSource as IList);
            unitcomboBox.Text = ServiceRegistry.GetInstance().GetDefaultDto(unitcomboBox.DataSource as IList);
            currencycomboBox.Text = ServiceRegistry.GetInstance().GetDefaultDto(currencycomboBox.DataSource as IList);

            udataGridViewComboBoxColumnConvUnit.DataSource = Utils.GetListCode(m_logic.UnitList);
            vdataGridViewComboBoxColumnVendorName.DataSource= Utils.GetListCode(m_logic.GetVendorList());
            sdataGridViewComboBoxColumn1.DataSource=Utils.GetListCode(m_logic.GetPartServiceList());

            comboBoxFieldInfo.DataSource = m_logic.FieldList;

        }

        public void Refresh()
        {
            b_init = false;
            Init();
            m_logic.Refresh();
        }

        public void ClearForm()
        {
            tabPage1.Text = string.Empty;
            textBoxCode.Text = string.Empty;
            textBoxName.Text = string.Empty;
            barcodetextBox.Text = string.Empty;
            partgroupcomboBox.SelectedItem = null;
            stocknumericUpDown.Value = 0m;
            minstocknumericUpDown.Value = 0m;
            maxstocknumericUpDown.Value = 0m;
            currencycomboBox.SelectedItem = null;
            unitcomboBox.SelectedItem = null;
            costpricenumericUpDown.Value = 0m;
            sellpricenumericUpDown.Value = 0m;
            notetextBox.Text = string.Empty;
            multiuomcheckBox.Checked = false;
            partTypecomboBox.SelectedItem = PartType.CustomerPart;
            decimalPlacenumericUpDown.Value = 0m;
            costMethodcomboBox.SelectedItem = CostMethod.MovingAverage;
            category1comboBox.SelectedItem = null;
            category2comboBox.SelectedItem = null;
            category3comboBox.SelectedItem = null;
            taxablecheckBox.Checked = false;
            discontinuecheckBox.Checked = false;
            statusBar1.Text = string.Empty;
            ClearUomList();
            ClearVendorPartNameList();
            ClearActivityList();//default

            partgroupcomboBox.Text = ServiceRegistry.GetInstance().GetDefaultDto(partgroupcomboBox.DataSource as IList);
            //unitcomboBox.Text = ServiceRegistry.GetInstance().GetDefaultDto(unitcomboBox.DataSource as IList);
            currencycomboBox.Text = ServiceRegistry.GetInstance().GetDefaultDto(currencycomboBox.DataSource as IList);
            errorProvider1.SetError(textBoxCode, "");
            errorProvider1.SetError(textBoxName, "");
        }

        public void SetToReadOnly(bool readOnly)
        {
            textBoxName.Enabled = !readOnly;
            textBoxCode.Enabled = !readOnly;
            barcodetextBox.Enabled = !readOnly;
            partgroupcomboBox.Enabled = !readOnly;
            //stocknumericUpDown.Enabled = !readOnly;
            minstocknumericUpDown.Enabled = !readOnly;
            maxstocknumericUpDown.Enabled = !readOnly;
            currencycomboBox.Enabled = !readOnly;
            unitcomboBox.Enabled = !readOnly;
            costpricenumericUpDown.Enabled = !readOnly;
            sellpricenumericUpDown.Enabled = !readOnly;
            partTypecomboBox.Enabled = !readOnly;
            decimalPlacenumericUpDown.Enabled = !readOnly;
            costMethodcomboBox.Enabled = !readOnly;
            //category1comboBox.Enabled = !readOnly;
            //category2comboBox.Enabled = !readOnly;
            //category3comboBox.Enabled = !readOnly;
            taxablecheckBox.Enabled = !readOnly;
            multiuomcheckBox.Enabled = !readOnly;
            discontinuecheckBox.Enabled = !readOnly;
            notetextBox.Enabled = !readOnly;
            //openingbutton.Enabled = !readOnly;
            //partPriceInformationbutton.Enabled = !readOnly;
            deleteUomToolStripMenuItem.Enabled = !readOnly;
            deleteVPNtoolStripMenuItem.Enabled = !readOnly;
            //udataGridViewComboBoxColumnConvUnit.ReadOnly = readOnly;
            //udataGridViewTextBoxColumnConvUnit.ReadOnly = readOnly;
            //udataGridViewTextBoxColumnCostPrice.ReadOnly = readOnly;
            udataGridViewTextBoxColumnOrigQty.ReadOnly = readOnly;
            //udataGridViewTextBoxColumnOrigUnit.ReadOnly = readOnly;
            udataGridViewTextBoxColumnSellPrice.ReadOnly = readOnly;


            vdataGridViewComboBoxColumnVendorName.ReadOnly = readOnly;
            vdataGridViewTextBoxColumnVendorPartname.ReadOnly = readOnly;
            sdataGridViewComboBoxColumn1.ReadOnly = readOnly;
            sdataGridViewTextBoxColumnQuantity.ReadOnly = readOnly;

            costpricenumericUpDown.DecimalPlaces = Currency == null ? 2 : Currency.DecimalPlace;
            sellpricenumericUpDown.DecimalPlaces = Currency == null ? 2 : Currency.DecimalPlace;

            udataGridViewTextBoxColumnCostPrice.DefaultCellStyle.Format = "N" + (Currency == null ? 2 : Currency.DecimalPlace);
            udataGridViewTextBoxColumnSellPrice.DefaultCellStyle.Format = "N" + (Currency == null ? 2 : Currency.DecimalPlace);

            udataGridViewComboBoxColumnConvUnit.ReadOnly = readOnly ? readOnly : !multiuomcheckBox.Checked;
            udataGridViewTextBoxColumnConvUnit.ReadOnly = readOnly ? readOnly : !multiuomcheckBox.Checked;
            udataGridViewTextBoxColumnCostPrice.ReadOnly = readOnly ? readOnly : !multiuomcheckBox.Checked;

            bool categoryEnable = m_mainForm.GetGeneralSetup().PartCategory;
            category1comboBox.Enabled = !readOnly && m_mainForm.GetGeneralSetup().PartCategory1 && categoryEnable;
            category2comboBox.Enabled = !readOnly && m_mainForm.GetGeneralSetup().PartCategory2 && categoryEnable;
            category3comboBox.Enabled = !readOnly && m_mainForm.GetGeneralSetup().PartCategory3 && categoryEnable;

            m_readonly = readOnly;
        }

        public void SetButtonEditMode(Premiere.Window.EditMode editMode)
        {
            toolBarButtonNew.Enabled = buttonNew.Enabled = ((editMode.Equals(EditMode.OK) || editMode.Equals(EditMode.Empty))) && m_mainForm.GetRules(this.Name).New;
            toolBarButtonSave.Enabled = buttonSave.Enabled = ((editMode.Equals(EditMode.Edit) || editMode.Equals(EditMode.Insert))) && m_mainForm.GetRules(this.Name).New;
            toolBarButtonEdit.Enabled = buttonEdit.Enabled = ((editMode.Equals(EditMode.OK))) && m_mainForm.GetRules(this.Name).Edit;
            toolBarButtonDelete.Enabled = buttonDelete.Enabled = ((editMode.Equals(EditMode.OK) || editMode.Equals(EditMode.Edit))) && m_mainForm.GetRules(this.Name).Delete;
            toolBarButtonPrint.Enabled = m_mainForm.GetRules(this.Name).Print;
        }
        private void multiuomcheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (!multiuomcheckBox.Enabled) return;
            if (multiuomcheckBox.Checked)
            {
                ClearUomList();
              //  m_logic.AddNewUnitConversion();
                udataGridViewComboBoxColumnConvUnit.ReadOnly =!multiuomcheckBox.Checked;
                udataGridViewTextBoxColumnConvUnit.ReadOnly = !multiuomcheckBox.Checked;
                udataGridViewTextBoxColumnCostPrice.ReadOnly = !multiuomcheckBox.Checked;
            }
            else
            {
                ClearUomList();
            }
        }
        public void RemoveItem(long id)
        {
            if (m_items.ContainsKey(id))
            {
                ListViewItem item = (ListViewItem)m_items[id];
                listViewRecords.Items.Remove(item);
            }
        }
        #endregion

        #region IUserControlPart Members


        public string Barcode
        {
            get
            {
                return barcodetextBox.Text;
            }
            set
            {
                barcodetextBox.Text = value;
            }
        }

        public PartGroupDTO PartGroup
        {
            get
            {
                return (PartGroupDTO)partgroupcomboBox.SelectedItem;
            }
            set
            {
                partgroupcomboBox.Text = value == null ? string.Empty : value.ToString();
            }
        }

        public decimal CurrentStock
        {
            get
            {
                return stocknumericUpDown.Value;
            }
            set
            {
                stocknumericUpDown.Value = value;
            }
        }

        public decimal MinimumStock
        {
            get
            {
                return minstocknumericUpDown.Value;
            }
            set
            {
                minstocknumericUpDown.Value = value;
            }
        }

        public decimal MaximumStock
        {
            get
            {
                return maxstocknumericUpDown.Value;
            }
            set
            {
                maxstocknumericUpDown.Value = value;
            }
        }

        public CurrencyDTO Currency
        {
            get
            {
                return (CurrencyDTO)currencycomboBox.SelectedItem;
            }
            set
            {
                currencycomboBox.Text = value == null ? string.Empty : value.ToString();
            }
        }

        public UnitDTO Unit
        {
            get
            {
                return (UnitDTO)unitcomboBox.SelectedItem;
            }
            set
            {
                unitcomboBox.Text = value == null ? string.Empty : value.ToString();
            }
        }

        public decimal CostPrice
        {
            get
            {
                return costpricenumericUpDown.Value;
            }
            set
            {
                costpricenumericUpDown.Value = value;
            }
        }

        public decimal SellPrice
        {
            get
            {
                return sellpricenumericUpDown.Value;
            }
            set
            {
                sellpricenumericUpDown.Value = value;
            }
        }
        public PartType PartType
        {
            get
            {
                return (PartType)partTypecomboBox.SelectedItem;
            }
            set
            {
                partTypecomboBox.SelectedItem = value;
            }
        }

        public int DecimalPlace
        {
            get
            {
                return Convert.ToInt16(decimalPlacenumericUpDown.Value);
            }
            set
            {
                decimalPlacenumericUpDown.Value = value;
            }
        }

        public CostMethod CostMethod
        {
            get
            {
                return (CostMethod)costMethodcomboBox.SelectedItem;
            }
            set
            {
                costMethodcomboBox.SelectedItem = value;
            }
        }

        public PartCategoryDTO PartCategory1
        {
            get
            {
                return (PartCategoryDTO)category1comboBox.SelectedItem;
            }
            set
            {
                category1comboBox.Text = value == null ? string.Empty : value.ToString();
            }
        }
        public PartCategoryDTO PartCategory2
        {
            get
            {
                return (PartCategoryDTO)category2comboBox.SelectedItem;
            }
            set
            {
                category2comboBox.Text = value == null ? string.Empty : value.ToString();
            }
        }
        public PartCategoryDTO PartCategory3
        {
            get
            {
                return (PartCategoryDTO)category3comboBox.SelectedItem;
            }
            set
            {
                category3comboBox.Text = value == null ? string.Empty : value.ToString();
            }
        }
        public bool Taxable
        {
            get
            {
                return taxablecheckBox.Checked;
            }
            set
            {
                taxablecheckBox.Checked = value;
            }
        }

        public bool Multiuom
        {
            get
            {
                return multiuomcheckBox.Checked;
            }
            set
            {
                multiuomcheckBox.Checked = value;
            }
        }

        public bool Discontinue
        {
            get
            {
                return discontinuecheckBox.Checked;
            }
            set
            {
                discontinuecheckBox.Checked = value;
            }
        }
        public string Note
        {
            get
            {
                return notetextBox.Text;
            }
            set
            {
                notetextBox.Text = value;
            }
        }


        public IList GetListUom()
        {
            IList list = new ArrayList();
            foreach (DataGridViewRow rw in dataGridViewUOM.Rows)
            {
                //if (rw.Tag == null) continue;
                if (dataGridViewUOM[1, rw.Index].Value == null) continue;
                UnitConversionDTO unit = (UnitConversionDTO)rw.Tag;
                if (unit == null)
                {
                    unit = new UnitConversionDTO();
                    rw.Tag = unit;
                }
                unit.ConversionQty = Convert.ToDecimal(dataGridViewUOM[0, rw.Index].Value);
                unit.ConversionUnit = (UnitDTO)Utils.FindDTOInList(dataGridViewUOM[1, rw.Index].Value.ToString(),m_logic.UnitList);//ServiceRegistry.GetInstance().GetUnitService().FindByCode(dataGridViewUOM[1, rw.Index].Value.ToString());//(UnitDTO)dataGridViewUOM[1, rw.Index].Value;
                unit.OriginalQty = Convert.ToDecimal(dataGridViewUOM[2, rw.Index].Value);
                unit.CostPrice = Convert.ToDecimal(dataGridViewUOM[4, rw.Index].Value);
                unit.SellPrice = Convert.ToDecimal(dataGridViewUOM[5, rw.Index].Value);
                if (unit.ConversionUnit == null) continue;
                list.Add(unit);
            }
            return list;
        }

        public IList GetListVendorPartName()
        {
            IList list = new ArrayList();
            foreach (DataGridViewRow rw in dataGridViewVendorPartName.Rows)
            {
                //if (rw.Tag == null) continue;
                if (dataGridViewVendorPartName[0, rw.Index].Value == null) continue; ;
                VendorPartNameDTO dto = (VendorPartNameDTO)rw.Tag;
                if (dto == null)
                {
                    dto = new VendorPartNameDTO();
                    rw.Tag = dto;
                }
                dto.Vendor = (VendorDTO)Utils.FindDTOInList(dataGridViewVendorPartName[0, rw.Index].Value.ToString(),m_logic.GetVendorList());//ServiceRegistry.GetInstance().GetCustomerService().FindByCode(dataGridViewVendorPartName[0, rw.Index].Value.ToString());// (VendorDTO)dataGridViewVendorPartName[0, rw.Index].Value;
                if(dto.Vendor ==null)
                    dto.Vendor=(VendorDTO)ServiceRegistry.GetInstance().GetSupplierService().FindByCode(dataGridViewVendorPartName[0, rw.Index].Value.ToString());
                dto.Name = dataGridViewVendorPartName[1, rw.Index].Value.ToString();
                if (dto.Vendor == null) continue;
                list.Add(dto);
            }
            return list;
        }

        public IList GetListServicePart()
        {
            IList list = new ArrayList();
            foreach (DataGridViewRow rw in dataGridViewService.Rows)
            {
                //if (rw.Tag == null) continue;
                if (dataGridViewService[0, rw.Index].Value == null) continue;
                ServicePartDTO dto = (ServicePartDTO)rw.Tag;
                if (dto == null)
                {
                    dto = new ServicePartDTO();
                    rw.Tag = dto;
                }
                dto.PartService = (PartDTO)Utils.FindDTOInList(dataGridViewService[0, rw.Index].Value.ToString(),m_logic.GetPartServiceList());//ServiceRegistry.GetInstance().GetPartService().FindByCode(dataGridViewService[0, rw.Index].Value.ToString());
                dto.Name = dataGridViewService[1, rw.Index].Value.ToString();
                if (dto.PartService == null) continue;
                list.Add(dto);
            }
            return list;
        }

        public void ClearUomList()
        {
            dataGridViewUOM.Rows.Clear();
        }

        public void ClearVendorPartNameList()
        {
            dataGridViewVendorPartName.Rows.Clear();
        }

        public void ClearServicerPartList()
        {
            dataGridViewService.Rows.Clear();
        }

        public void ClearActivityList()
        {
            listViewActivities.Items.Clear();
        }

        public void AddUOM(UnitConversionDTO u)
        {
            int index = dataGridViewUOM.Rows.Add(u.ConversionQty, u.ConversionUnit.Code, u.OriginalQty, 
                u.Part.Unit.Code, u.CostPrice, u.SellPrice);
            dataGridViewUOM.Rows[index].Tag = u;
        }

        public void AddVendorPartName(VendorPartNameDTO u)
        {
            int index = dataGridViewVendorPartName.Rows.Add(u.Vendor.Code, u.Name);
            dataGridViewVendorPartName.Rows[index].Tag = u;
        }

        public void AddServicePart(ServicePartDTO sp)
        {
            int index = dataGridViewService.Rows.Add(sp.PartService.Code, sp.Quantity);
            dataGridViewService.Rows[index].Tag = sp;
        }

        public void AddActivity(StockCardEntryInfoDTO activity)
        {
            ListViewItem item = listViewActivities.Items.Add(activity.TransactionDate.ToString("dd-MMM-yyyy"));
            item.SubItems.Add(activity.EntryType.ToString());
            item.SubItems.Add(activity.Quantity.ToString());
            item.SubItems.Add(activity.Unit.Name.ToString());
            item.SubItems.Add(activity.Vendor.Name.ToString());
            item.SubItems.Add(activity.EventStatus.ToString());
            item.Tag = activity;
        }

        #endregion
        private void currencycomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox s = (ComboBox)sender;
            if (s.SelectedIndex < 0) return;
            costpricenumericUpDown.DecimalPlaces = Currency.DecimalPlace;
            sellpricenumericUpDown.DecimalPlaces = Currency.DecimalPlace;
            udataGridViewTextBoxColumnCostPrice.DefaultCellStyle.Format = "N" + (Currency == null ? 2 : Currency.DecimalPlace);
            udataGridViewTextBoxColumnSellPrice.DefaultCellStyle.Format = "N" + (Currency == null ? 2 : Currency.DecimalPlace);
        }

        private void buttonFind_Click(object sender, EventArgs e)
        {
            m_logic.Find();
        }

        #region IUserControlPart Members


        public void ClearRecordList()
        {
            listViewRecords.Items.Clear();
            m_items.Clear();
        }

        public FieldInfo FieldInfo
        {
            get { return (FieldInfo)comboBoxFieldInfo.SelectedItem; }
        }

        public string Filter
        {
            get { return searchTextBox1.Text; }
        }

        public bool Ascending
        {
            get { return false; }
        }

        public string FoundRecord
        {
            set { labelRecordFound.Text = value; }
        }

        public void FocusToCode()
        {
            textBoxCode.Focus();
        }

        public bool IsValidSubject()
        {
            errorProvider1.SetError(textBoxCode, "");
            errorProvider1.SetError(textBoxName, "");
            bool a = textBoxCode.Text == string.Empty;
            bool b = textBoxName.Text == string.Empty;
            if (a) errorProvider1.SetError(textBoxCode, "Please fill Code");
            if (b) errorProvider1.SetError(textBoxName, "Please fill Name");
            return !a && !b;
        }

        #endregion

        private void button2_Click(object sender, EventArgs e)
        {
            MarkDownSellingPriceForm mrk = new MarkDownSellingPriceForm(m_logic.GetSubject(), m_mainForm);
            mrk.ShowDialog();
            if (m_logic.GetSubject().Id == 0) return;
            IDTO part = ServiceRegistry.GetInstance().GetPartService().Get(m_logic.GetSubject().Id);
            m_logic.UpdateSubject(m_logic.GetSubject());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_logic.UpdateSubject();
            PartPriceInformationListForm frm = new PartPriceInformationListForm(m_logic.GetSubject(), m_readonly);
            frm.ShowDialog();
        }

        private void textBoxFilter_TextChanged(object sender, EventArgs e)
        {
            if (m_key == Keys.Enter)
            {
                if (searchTextBox1.Text == string.Empty)
                {
                    return;
                }
                m_logic.Find();
            }
        }
        Keys m_key = Keys.A;

        private void textBoxFilter_KeyUp(object objSender, KeyEventArgs objArgs)
        {
            m_key = objArgs.KeyData;
            if (m_key == Keys.Enter)
            {
                if (searchTextBox1.Text == string.Empty)
                {
                    return;
                }
                m_logic.Find();
            }
        }

        private void deleteUomToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dataGridViewUOM.Rows.Remove(dataGridViewUOM.CurrentRow);
        }

        private void deleteVPNtoolStripMenuItem_Click(object sender, EventArgs e)
        {
            dataGridViewVendorPartName.Rows.Remove(dataGridViewVendorPartName.CurrentRow);
        }

        private void menuItemDeleteServicePart_Click(object sender, EventArgs e)
        {
            dataGridViewService.Rows.Remove(dataGridViewService.CurrentRow);
        }

        private void LoadAlllinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            m_logic.LoadAllPart();
        }

        private void searchTextBox1_Search(object sender, EventArgs e)
        {
            if (searchTextBox1.Text == string.Empty)
            {
                return;
            }
            m_logic.Find();
        }
    }
}